//
//  SCSearchViewController.m
//  ShangChengMoBan
//
//  Created by yaoqi yaoqi on 16/10/8.
//  Copyright © 2016年 ping. All rights reserved.
//

#import "SCSearchViewController.h"
#import "SCPaiXuView.h"
#import "ProductCollectionViewCell.h"
#import "FL_ChanPinModel.h"
#import "SCHomeViewController.h"
@interface SCSearchViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UITextFieldDelegate>
{
    RWcollectionView *_productCollecTionView;
    NSMutableArray *_productAry;
    SCPaiXuView *_paiXuView;
    UITextField * _field;
    UIButton * _remove;
    int _page;
    int _scortPage;
}


@end

@implementation SCSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    _scortPage = 1;
    [self drawView];
    [self drawPaiXuView];
    [self drawBackButton];
    self.view.backgroundColor = [UIColor whiteColor];
    _field = [[UITextField alloc]initWithFrame:CGRectMake(50, 30, KScreenWidth-110, 20)];
    _field.font = [UIFont systemFontOfSize:14];
    _field.delegate = self;
    [_field becomeFirstResponder];
    _field.placeholder = @"搜索宝贝";
    [self.view addSubview:_field];
    UIView *xin = [[UIView alloc]initWithFrame:CGRectMake(50, _field.bottom+1, KScreenWidth-110, 1.5)];
    xin.backgroundColor =  [UIColor redColor];
    [self.view addSubview:xin];
    UIButton *search = [[UIButton alloc]initWithFrame:CGRectMake(_field.right+12, 30, 35, 23)];
    search.layer.cornerRadius = 4;
    [search setTitle:@"搜索" forState:UIControlStateNormal];
    search.backgroundColor = [UIColor redColor];
    [search setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    search.titleLabel.font = [UIFont systemFontOfSize:14];
    [search addTarget:self action:@selector(search1) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:search];
    _remove = [[UIButton alloc]initWithFrame:CGRectMake(xin.right-15, 33, 15, 15)];
    [_remove setBackgroundImage:[UIImage imageNamed:@"关闭_"] forState:UIControlStateNormal];
    [_remove addTarget:self action:@selector(remove) forControlEvents:UIControlEventTouchUpInside];
    _remove.hidden = YES;
    [self.view addSubview:_remove];
}
-(void)search1{
 [self requestDataWith:1];
    _productCollecTionView.backgroundColor = FenLeiBeiJingHuiSe;
    _paiXuView.hidden = NO;
}
-(void)remove{
    _field.text = @"";
    _remove.hidden = YES;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
         _remove.hidden = NO;
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
 
}
-(void)drawPaiXuView{
    _paiXuView = [[SCPaiXuView alloc]initWithFrame:CGRectMake(0, 64, KScreenWidth, 40 * _kscH)];
    _paiXuView.hidden = YES;
    [_paiXuView.xiaoLiangbtn addTarget:self action:@selector(xiaoLiangClicked) forControlEvents:UIControlEventTouchUpInside];
    
    [_paiXuView.jiaGeBtn addTarget:self action:@selector(jiaGeClicked) forControlEvents:UIControlEventTouchUpInside];
    [_paiXuView.PingjiaBtn addTarget:self action:@selector(pingJiaClicked) forControlEvents:UIControlEventTouchUpInside];
    [_paiXuView.zongHeBtn addTarget:self action:@selector(zongHeBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_paiXuView];
}

-(void)requestDataWith:(int)first{
    
    NSMutableDictionary *requestDic = [[NSMutableDictionary alloc]init];
    [requestDic setObject:[NSString stringWithFormat:@"%d",_page] forKey:@"pageNum"];
    [requestDic setObject:@"10" forKey:@"pageSize"];
    [requestDic setObject:_field.text forKey:@"goodsName"];
     [requestDic setObject:[NSString stringWithFormat:@"%d",_scortPage] forKey:@"sortType"];
    [NetWorking NEWjieKouQingQiu:FL_findByCategory ANDIP:IP And:requestDic anBlock:^(NSDictionary *dic, NSError *error) {
        NSLog(@"%@",dic[Msg]);
        [_productCollecTionView headerEndRefreshing];
        [_productCollecTionView footerEndRefreshing];
        if (!error) {
            if ([[dic objectForKey:Code]intValue] == 200) {
                NSArray *data = dic[Response][@"resultList"];
                
                if (first == 1) {
                    _productAry = [[NSMutableArray alloc]init];
                }
                
                [data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *  stop) {
                    FL_ChanPinModel *chanPinModel = [FL_ChanPinModel objectFromDictionary:obj];
                    [_productAry addObject:chanPinModel];
                }];
                
                [_productCollecTionView reloadData];
            }
        }
    }];
}

-(void)chanPaiXuColor{
    [_paiXuView.zongHeBtn setTitleColor:MaintextColor forState:UIControlStateNormal];
    [_paiXuView.xiaoLiangbtn setTitleColor:MaintextColor forState:UIControlStateNormal];
    [_paiXuView.jiaGeBtn setTitleColor:MaintextColor forState:UIControlStateNormal];
    _paiXuView.xiaoLiangimageView.image = [UIImage imageNamed:@"buttom"];
    _paiXuView.jiaGeimageView.image = [UIImage imageNamed:@"buttom"];
}

-(void)xiaoLiangClicked{
    [self chanPaiXuColor];
    if (_scortPage == 3) {
        _scortPage = 4;
        _paiXuView.xiaoLiangimageView.image = [UIImage imageNamed:@"topsmall"];
    }else{
        _scortPage = 3;
        _paiXuView.xiaoLiangimageView.image = [UIImage imageNamed:@"buttom"];
    }
    
    
    [_paiXuView.xiaoLiangbtn setTitleColor:ZONGCOLOR forState:UIControlStateNormal];
    
    [self requestDataWith:1];
}

-(void)jiaGeClicked{
    [self chanPaiXuColor];
    if (_scortPage == 1) {
        _scortPage = 2;
        _paiXuView.jiaGeimageView.image = [UIImage imageNamed:@"topsmall"];
    }else{
        _scortPage = 1;
        _paiXuView.jiaGeimageView.image = [UIImage imageNamed:@"buttom"];
    }
    
    
    [_paiXuView.jiaGeBtn setTitleColor:ZONGCOLOR forState:UIControlStateNormal];
    [self requestDataWith:1];
}

-(void)zongHeBtnClicked{
    _scortPage = 1;
    
    [self chanPaiXuColor];
    [_paiXuView.zongHeBtn setTitleColor:ZONGCOLOR forState:UIControlStateNormal];
    [self requestDataWith:1];
}

-(void)pingJiaClicked{
    
}

-(void)drawView{
    UICollectionViewFlowLayout *flow1 = [[UICollectionViewFlowLayout alloc]init];
    //    flow.itemSize = CGSizeMake(KScreenWidth/2, 240);
    flow1.minimumInteritemSpacing = 10 * _kscW;
    flow1.minimumLineSpacing = 1;
    
    _productCollecTionView = [[RWcollectionView alloc]initWithFrame:CGRectMake(0, 64 + 40 * _kscH, KScreenWidth, KScreenHeight - 64 - 30 * _kscH-10) collectionViewLayout:flow1];
    _productCollecTionView.delegate = self;
    _productCollecTionView.dataSource = self;
    //    [_shangChengCollecTionView addSubview:_shangChengGGScrollview];
    
    _productCollecTionView.backgroundColor = [UIColor whiteColor];
    [_productCollecTionView registerClass:[ProductCollectionViewCell class] forCellWithReuseIdentifier:@"ProductCollectionViewCell"];
    
    [_productCollecTionView addHeaderWithTarget:self action:@selector(insertRowAtTop)];
    
    [_productCollecTionView addFooterWithTarget:self action:@selector(insertRowAtBottom)];
    
    [self.view addSubview:_productCollecTionView];
    
}

#pragma mark collectionView数据源

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _productAry.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ProductCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ProductCollectionViewCell" forIndexPath:indexPath];
    
    FL_ChanPinModel *proDuctInfo = [_productAry objectAtIndex:indexPath.item];
    
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:[self requestImageStr]]];
    
    cell.nameLabel.text = proDuctInfo.name;
    
    cell.jiaGeLabel.text = [NSString stringWithFormat:@"¥%.1f",[proDuctInfo.ourPrice floatValue]];
    NSMutableAttributedString *  attribtStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"¥%.1f",[proDuctInfo.originalPrice floatValue]]];
    [attribtStr addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0,attribtStr.length)];
    cell.yuanJiaGeLabel.attributedText = attribtStr;  //商品原价
    
    return cell;
}

-(void)insertRowAtTop{
    _page = 1;
    [self requestDataWith:1];
}

-(void)insertRowAtBottom{
    _page++;
    [self requestDataWith:0];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{
    
    return CGSizeMake((KScreenWidth-30 * _kscW)/ 2, 172 * _kscH);
}


//设置整个分区相对上下左右的间距

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{
    
    return UIEdgeInsetsMake(0, 10 * _kscW, 0, 10 * _kscW);// top, left, bottom, right
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    //    SP_ProductModel *proDuctInfo = [_productAry objectAtIndex:indexPath.item];
    //
    //    ProductViewController *product = [[ProductViewController alloc] init];
    //
    //    product.idStr = proDuctInfo._id;
    //
    //    //        product.shangPuID = proDuctInfo.proShopId;
    //    [self.navigationController pushViewController:product animated:YES];
    
    SCHomeViewController *productView = [[SCHomeViewController alloc]init];
    productView.FL_chanPin = [_productAry objectAtIndex:indexPath.item];
    [self.navigationController pushViewController:productView animated:YES];
    
}


@end
